package com.xzmzhp.controller.finance;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.finance.Finance;
import com.xzmzhp.service.finance.FinanceService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import com.xzmzhp.vo.finance.FinanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-03-27 16:57
 * @program: ams
 * @description: 财务信息前端交互类
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/finance")
public class FinanceController {

    @Autowired
    private FinanceService financeService;

    /**
     * 查询所有财务数据
     * @return 财务信息
     */
    @GetMapping("/findAll")
    public ReturnData findAll(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "limit", defaultValue = "10") int limit){
        //开启分页
        PageHelper.startPage(page, limit, true);
        //查询数据
        List<FinanceVo> list = financeService.findAll();
        if (StringUtils.isNotEmpty(list)){
            //返回查询信息
            PageInfo<FinanceVo> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(pageInfo);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询失败，请重试");
    }

    /**
     * 根据条件查询财务信息
     *
     * @param params 参数
     *        cashFlow  收支类型
     *        financesTypeId 收支项目id
     *        financesPass 是否通过
     *        personnelName  收支人员姓名
     *        startTime 起始时间
     *        endTime 结束时间
     * @return 相应财务信息
     */
    @GetMapping("/findFinanceVoByParam")
    public ReturnData findFinanceVoByParam(@RequestParam(value = "page", defaultValue = "1") int page,
                                           @RequestParam(value = "limit", defaultValue = "10") int limit,
                                           @RequestBody Map<String,Object> params) {
        //开启分页
        PageHelper.startPage(page, limit, true);
        //查询数据
        List<FinanceVo> list = financeService.findFinanceVoByParam(params);
        if (StringUtils.isNotEmpty(list)) {
            //返回查询信息
            PageInfo<FinanceVo> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(pageInfo);
        }
        return ReturnData.error(ReturnConstant.FAIL, "查询失败，请重试");
    }

    /**
     * 添加财务信息
     * @param finance 财务信息
     * @return 是否成功
     */
    @GetMapping("/addFinance")
    public ReturnData addFinance(@RequestBody Finance finance){
        //添加财务信息
        Integer i = financeService.addFinance(finance);
        if (i > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "财务类型信息修改错误");
        }
    }

    /**
     * 修改财务信息
     * @param finance 财务信息
     * @return 是否成功
     */
    @GetMapping("/exitFinance")
    public ReturnData exitFinance(@RequestBody Finance finance){
        //添加财务信息
        Integer i = financeService.exitFinance(finance);
        if (i > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "财务类型信息修改错误");
        }
    }

}
